﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TCPIPClient;
using TCPIPServer;

namespace Communication
{
    public class TCPIPSocket
    {
        commClient _TCPClient = null;
        commServer _TCPServer = null;
        bool _isServer = false;

        string _serverIP = "";
        int _Port = 0;
        string _AppID = "";


        public TCPIPSocket(string serverIP, int Port, string AppID)
        {
            this._isServer = false;
            this._serverIP = serverIP;
            this._Port = Port;
            this._AppID = AppID;

            this._TCPClient = new commClient();
            this._TCPClient.TransmissionReceived += TCP_TransmissionReceived;
        }

        public TCPIPSocket(int Port, string AppID)
        {
            this._isServer = true;
            this._Port = Port;
            this._AppID = AppID;

            this._TCPServer = new commServer();
            this._TCPServer.TransmissionReceived += TCP_TransmissionReceived;
            this._TCPServer.NewClientConnected += _TCPServer_NewClientConnected;
        }

        public event commServer.delegateNewClientConnected TCPServerNewClientConnected;
        private void _TCPServer_NewClientConnected(string text)
        {
            if (TCPServerNewClientConnected != null)
            {
                TCPServerNewClientConnected(text);
            }
        }

        public event commClient.delegateTransmissionReceived TCPTransmissionReceived;
        private void TCP_TransmissionReceived(string text)
        {
            if (TCPTransmissionReceived != null)
            {
                if ((text.StartsWith(this._AppID))||(text.StartsWith("ALL")))
                {
                    TCPTransmissionReceived(text);
                } else {
                    if (this._isServer == true)
                    {
                        commServer.broadcast(text);
                    }
                    
                }
            }
        }

        public bool Connect()
        {
            if (this._isServer)
            {
                this._TCPServer.StartListening(this._Port);
                return true;
            } else
            {
                return this._TCPClient.Connect(this._serverIP, this._Port, this._AppID);
            }
        }

        public void SendData(string data)
        {
            if (this._isServer)
            {
                commServer.broadcast(data);
            } else
            {
                this._TCPClient.Send(data);
            }
        }

        public bool Disconnect()
        {
            if (this._isServer)
            {
                this._TCPServer.StopListening();
                return true;
            } else
            {
                return this._TCPClient.Disconnect();
            }
        }

        public bool IsConnected
        {
            get
            {
                if (this._isServer)
                {
                    return this._TCPServer.IsListening;
                } else
                {
                    return this._TCPClient.IsConnected;
                }
            }
        }

    }
}
